package com.nanfang51g3.ckz.ui.testui;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.AndroidCharacter;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.adapter.ext.HomeRightAdapter;
import com.nanfang51g3.ckz.adapter.ext.IndexAdapter;
import com.nanfang51g3.ckz.domain.IndexBean;
import com.nanfang51g3.ckz.domain.RightTypeBean;
import com.nanfang51g3.ckz.utils.GeoHashKit;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.widget.empty.PageStateLayout;
import com.nanfang51g3.ckz.widget.empty.utils.PageState;
import com.nanfang51g3.ckz.widget.progress.NumberProgressBar;
import com.nanfang51g3.ckz.widget.progress.OnProgressBarListener;
import com.nanfang51g3.ckz.widget.temptyview.TEmptyView;
import com.nanfang51g3.ckz.widget.temptyview.TViewUtil;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2016/5/18.
 */
public class TestActivity extends AppCompatActivity implements OnProgressBarListener {
//        @Bind(R.id.home_recycler)
//    RecyclerView homeRecycler;
//    @Bind(R.id.drawer_layout)
//    DrawerLayout drawerLayout;
//    @Bind(R.id.home_view)
//    RelativeLayout homeView;
//    private Timer timer;


    Button bt_show;
    private NumberProgressBar bnp;
    RecyclerView recyclerView;
    HomeRightAdapter indexAdapter;
    ArrayList<RightTypeBean> releaseInfoS = new ArrayList<>();
    CompositeSubscription _sub = new CompositeSubscription();

    @Override
    protected void onResume() {
        super.onResume();
//        _sub.add(new RxTest().textRx()
//                .asObservable()
//             /*   .subscribeOn(Schedulers.io())*/
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<String>() {
//            @Override
//            public void onCompleted() {
//                KLog.i("Evd", "onCompleted");
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                KLog.i("Evd", "onError " + e.toString());
//            }
//
//            @Override
//            public void onNext(String s) {
//                KLog.i("Evd", "onNext " + s.toString());
//            }
//        }));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        timer.cancel();
        KLog.i("Evd", "onDestroy");
        if (!_sub.isUnsubscribed()) {
            KLog.i("Evd", "unsubscribe");
            _sub.unsubscribe();
        }
    }

    public void setNext(int i) {
        KLog.i("FXX", "setNext  i value = " + i);
    }

    private static final int LOADING = 0;
    private static final int EMPTY = 1;
    private static final int ERROR = 2;
    private static final int SUCCEED = 3;
    private static final int REQUESTING = 4;
    private long msgDelayed = 4000;

    private PageStateLayout mLayout;
    LinearLayout linear;
    private PageState currentState = PageState.SUCCEED;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.progess);


//        bt_show = (Button) findViewById(R.id.bt_show);
//        bt_show.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                for (int i = 0; i < 10; i++) {
//                    int selector = i;
//                    KLog.i("FXX", "for value = " + i);
//                    setNext(i);
//                }
//                if (releaseInfoS.isEmpty()) {
//                    for (int i = 0; i < 10; i++) {
//                        releaseInfoS.add(new RightTypeBean("1", false));
//                    }
//                    indexAdapter.addList(releaseInfoS);
//                }else{
//                    indexAdapter.clearList();
//                }
//
//            }
//        });
        recyclerView = (RecyclerView) findViewById(R.id.home_recycler);
        indexAdapter = new HomeRightAdapter(releaseInfoS);

//        for (int i = 0; i < 20; i++) {
//            releaseInfoS.add(new RightTypeBean("1", false));
//        }
        linear = (LinearLayout) findViewById(R.id.linear);
        mLayout = new PageStateLayout(this);
        mLayout.setOnEmptyListener(mOnEmptyListener)
                .setOnErrorListener(mOnErrorListener)
                .load(linear, recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getApplicationContext()));
        recyclerView.setAdapter(indexAdapter);
        mLayout.onLoading();
//        TViewUtil.EmptyViewBuilder.getInstance(this.getApplicationContext())
//                .setShowIcon(true)
//                .setShowText(false)
//                .setShowButton(true)
//                .setAction(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Toast.makeText(TestActivity.this, "ss", Toast.LENGTH_SHORT).show();
//                        GeoHashKit ghf = new GeoHashKit();
//                        String gc1 = ghf.encode(43.5886130000, 87.7874830000);
//                        String gc2 = ghf.encode(51.4797, -0.0124);
//
//                        KLog.i("Evd", gc1);
//                        KLog.i("Evd", gc2);
//
//                        double[] gd1 = ghf.decode(gc1);
//                        double[] gd2 = ghf.decode(gc2);
//                        KLog.i("Evd", gd1[0] + ", " + gd1[1]);
//                        KLog.i("Evd", gd2[0] + ", " + gd2[1]);
//                    }
//                })
//                .bindView(recyclerView);

//        ButterKnife.bind(this);
//        drawerLayout.setScrimColor(Color.TRANSPARENT);
//        initDrawerListenner();
//        ArrayList<RightTypeBean> mIndos = new ArrayList<>();
//        for (int i = 0; i < 25; i++) {
//
//            mIndos.add(new RightTypeBean());
//        }
//        HomeRightAdapter homeRightAdapter = new HomeRightAdapter(mIndos/*, this*/);
//        homeRightAdapter.setActivity(this);
//        homeRecycler.setHasFixedSize(true);//固定大小
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        homeRecycler.setLayoutManager(linearLayoutManager);
//        //间隔线条
////        heSuanRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
//        homeRecycler.setAdapter(homeRightAdapter);


//        bnp = (NumberProgressBar) findViewById(R.id.numberbar1);
//        bnp.setOnProgressBarListener(this);
//        timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        bnp.incrementProgressBy(1);
//                    }
//                });
//            }
//        }, 1000, 100);
    }

    private View.OnClickListener mOnEmptyListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(TestActivity.this, "Do something when empty", Toast.LENGTH_SHORT).show();
        }
    };

    private View.OnClickListener mOnErrorListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(TestActivity.this, "Do something when error", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onProgressChange(int current, int max) {
        if (current == max) {
            Toast.makeText(getApplicationContext(), +current + "---" + max, Toast.LENGTH_SHORT).show();
            bnp.setProgress(0);
        }
    }


    /**
     * 注册滑动组件监听
     */
    @SuppressLint("NewApi")
    private void initDrawerListenner() {
        // TODO Auto-generated method stub
        //滑动组件添加监听事件

//
//        //通过加载XML动画设置文件来创建一个Animation对象；
//        Animation animation= AnimationUtils.loadAnimation(this, R.anim.slide_right);   //得到一个LayoutAnimationController对象；
//        LayoutAnimationController controller = new LayoutAnimationController(animation);   //设置控件显示的顺序；
//        controller.setOrder(LayoutAnimationController.ORDER_REVERSE);   //设置控件显示间隔时间；
//        controller.setDelay(0.3f);   //为ListView设置LayoutAnimationController属性；
//        drawerLayout.setLayoutAnimation(controller);
//        drawerLayout.startLayoutAnimation();
//
//        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
//
//            @Override
//            public void onDrawerStateChanged(int arg0) {
//                // TODO Auto-generated method stub
//            }
//
//            @SuppressLint("NewApi")
//            @Override
//            public void onDrawerSlide(View drawerView, float slideOffset) {
//                // TODO 正在滑动监听方法
//                /*
//                 * drawerView:滑动的视图左侧或者右侧
//				 * slideOffset:滑动进度值
//				 */
//                KLog.i("TAG", "slideOffset=" + slideOffset);
//                //设置滑动时滑动视图的透明度
//
//
////				drawerView.setAlpha(slideOffset);
//                //判断滑动的视图
//                if (drawerView.getTag().equals("Left")) {
//					//设置X轴中心点
//                    homeView.setPivotX(homeView.getMeasuredWidth());
//					//如果滑动的是左视图，主布局向右移动当前滑动的距离(当前移动的距离=移动的视图drawerView*移动进度值slideOffset)
//                    homeView.scrollTo((int) (drawerView.getMeasuredWidth()*-slideOffset), 0);
//				}else{
//                    //设置X轴中心点
//                    homeView.setPivotX(0);
//                    //如果滑动的是右视图，主布局向左移动
//                    homeView.scrollTo((int) (drawerView.getMeasuredWidth() * slideOffset), 0);
//
//                }
//                //设置Y轴中心点
//                homeView.setPivotY(homeView.getMeasuredHeight() / 2);
//                //放缩比例值从原始值到1.0
////				scale=(1-basescale)*(1-slideOffset)+basescale;
//                //设置水平放缩比例值
////				mainlayout.setScaleX(scale);
//                //设置垂直放缩比例值
////				mainlayout.setScaleY(scale);
//            }
//
//            @Override
//            public void onDrawerOpened(View arg0) {
//                // TODO Auto-generated method stub
//            }
//
//            @Override
//            public void onDrawerClosed(View arg0) {
//                // TODO Auto-generated method stub
//            }
//        });
    }
}
